package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.nhi.could.waste.annotation.LoginWhitePathAnnotation;
import net.zigin.nhi.could.waste.dto.*;
import net.zigin.nhi.could.waste.model.WasteBase;
import net.zigin.nhi.could.waste.service.WasteBaseService;

import net.zigin.nhi.could.waste.service.WasteBaseRecordService;
import net.zigin.nhi.could.waste.util.EncryptDataUtil;
import net.zigin.nhi.could.waste.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * WasteBase控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: WasteBaseController
 * @date 2021-9-13 21:14:42
 */
@Api(tags = "医废管理")
@RestController
@RequestMapping("/wasteBase")
public class WasteBaseController extends BaseController {
    @Autowired
    private WasteBaseService wasteBaseService;
    @Autowired
    private WasteBaseRecordService wasteBaseRecordService;

    @GetMapping("/getById/{id}")
    @ApiOperation("查询医废详情")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String")
    public JsonForResult<WasteBaseVo> getById(@PathVariable String id) {
        AssertUtil.isNull(id, "用户id不能为空");
        WasteBase wasteBase = wasteBaseService.selectByPrimaryKey(id);
        WasteBaseVo wasteBaseVo = ReflectUtil.copyProperties(wasteBase, WasteBaseVo.class);
        return JsonForResult.<WasteBaseVo>builder().data(wasteBaseVo).success(true).build();
    }

    @GetMapping("/getListByBoxId/{boxId}")
    @ApiOperation("医废箱查询医废列表")
    @ApiImplicitParam(name = "boxId", value = "医废箱id", required = true, dataType = "String")
    public JsonForResult<List<WasteBaseVo>> getListByBoxId(@PathVariable String boxId) {
        List<WasteBaseVo> wasteBaseVos = wasteBaseService.getListByBoxId(boxId);
        return JsonForResult.<List<WasteBaseVo>>builder().data(wasteBaseVos).success(true).build();
    }

    @PostMapping("/getListByBoxQrCode")
    @ApiOperation("医废箱查询医废列表")
    @ApiImplicitParam(name = "qrCodeDto", value = "医废箱二维码", required = true, dataType = "QrCodeDto")
    public JsonForResult<Map<String, Object>> getListByBoxQrCode(@RequestBody QrCodeDto qrCodeDto) {
        Map<String, Object> info = wasteBaseService.getListByBoxQrCode(qrCodeDto.getContent());
        return JsonForResult.<Map<String, Object>>builder().data(info).success(true).build();
    }

    @PostMapping("/getPendingInStorageList")
    @ApiOperation("待入库列表")
    @ApiImplicitParam(name = "wasteInPutStorageDto", value = "医废入库详情", required = true, dataType = "WasteInPutStorageDto")
    public JsonForResult<Map<String, Object>> getPendingInStorageList(@RequestBody WasteInPutStorageDto wasteInPutStorageDto) {
        Map<String, Object> map = wasteBaseService.getPendingInStorageList(wasteInPutStorageDto);
        Integer count = wasteBaseService.getPendingInStorageListCount(wasteInPutStorageDto);
        return JsonForResult.<Map<String, Object>>builder().total(count).data(map).success(true).build();
    }

    @PostMapping("/getInfoByQrCode")
    @ApiOperation("二维码查询医废详情")
    @ApiImplicitParam(name = "qrCodeDto", value = "二位码信息", required = true, dataType = "QrCodeDto")
    public JsonForResult<WasteBaseVo> getInfoByQrCode(@RequestBody QrCodeDto qrCodeDto) {
        WasteBaseVo wasteBaseVo = wasteBaseService.getInfoByQrCode(qrCodeDto.getContent());
        return JsonForResult.<WasteBaseVo>builder().data(wasteBaseVo).success(true).build();
    }

    @GetMapping("/getPendingOutStorageList")
    @ApiOperation("待出库列表")
    public JsonForResult<List<WasteBaseVo>> getPendingOutStorageList() {
        List<WasteBaseVo> wasteBaseVos = wasteBaseService.getPendingOutStorageList();
        return JsonForResult.<List<WasteBaseVo>>builder().data(wasteBaseVos).total(wasteBaseVos.size()).success(true).build();
    }

    @PostMapping("/wasteCollect")
    @ApiOperation("医废收集")
    @ApiImplicitParam(name = "wasteCollectDto", value = "医废收集详情", required = true, dataType = "WasteCollectDto")
    public JsonForResult<String> wasteCollect(@RequestBody WasteCollectDto wasteCollectDto) {
        String id = wasteBaseService.wasteCollect(wasteCollectDto);
        return JsonForResult.<String>builder().data(id).success(true).build();
    }

    @PostMapping("/putSingleInStorage")
    @ApiOperation("扫码入库")
    @ApiImplicitParam(name = "wasteInPutStorageDto", value = "医废入库详情", required = true, dataType = "WasteInPutStorageDto")
    public JsonForResult<String> putSingleInStorage(@RequestBody WasteInPutStorageDto wasteInPutStorageDto) {
        wasteBaseService.putSingleInStorage(wasteInPutStorageDto);
        wasteBaseService.putSingleInStorage(wasteInPutStorageDto);
        return JsonForResult.<String>builder().success(true).build();
    }

    @PostMapping("/putMultipleInStorage")
    @ApiOperation("批量入库")
    @ApiImplicitParam(name = "wasteInPutStorageDto", value = "医废入库详情", required = true, dataType = "WasteInPutStorageDto")
    public JsonForResult<String> putMultipleInStorage(@RequestBody WasteInPutStorageDto wasteInPutStorageDto) {
        wasteBaseService.putMultipleInStorage(wasteInPutStorageDto);
        return JsonForResult.<String>builder().success(true).build();
    }

    @PostMapping("/putBoxInStorage")
    @ApiOperation("箱袋绑定入库")
    @ApiImplicitParam(name = "wasteInPutStorageDto", value = "医废入库详情", required = true, dataType = "WasteInPutStorageDto")
    public JsonForResult<String> putBoxInStorage(@RequestBody WasteInPutStorageDto wasteInPutStorageDto) {
        wasteBaseService.putBoxInStorage(wasteInPutStorageDto);
        return JsonForResult.<String>builder().success(true).build();
    }

    @PostMapping("/getSingleInStorageInfo")
    @ApiOperation("扫码入库医废详情")
    @ApiImplicitParam(name = "wasteInPutStorageDto", value = "医废入库详情", required = true, dataType = "WasteInPutStorageDto")
    public JsonForResult<WasteBaseVo> putSingleInStorageInfo(@RequestBody WasteInPutStorageDto wasteInPutStorageDto) {
        WasteBaseVo wasteBaseVo = wasteBaseService.getSingleInStorageInfo(wasteInPutStorageDto);
        return JsonForResult.<WasteBaseVo>builder().data(wasteBaseVo).success(true).build();
    }

    @PostMapping("/putMultipleInStorageInfo")
    @ApiOperation("批量入库医废列表详情")
    @ApiImplicitParam(name = "wasteInPutStorageDto", value = "医废入库详情", required = true, dataType = "WasteInPutStorageDto")
    public JsonForResult<List<WasteBaseVo>> putMultipleInStorageInfo(@RequestBody WasteInPutStorageDto wasteInPutStorageDto) {
        List<WasteBaseVo> baseVos = wasteBaseService.getMultipleInStorageInfo(wasteInPutStorageDto);
        return JsonForResult.<List<WasteBaseVo>>builder().data(baseVos).success(true).build();
    }

    @PostMapping("/getBoxInStorageInfo")
    @ApiOperation("箱袋绑定入库医废列表详情")
    @ApiImplicitParam(name = "wasteInPutStorageDto", value = "医废入库详情", required = true, dataType = "WasteInPutStorageDto")
    public JsonForResult<Map<String, Object>> getBoxInStorageInfo(@RequestBody WasteInPutStorageDto wasteInPutStorageDto) {
        Map<String, Object> info = wasteBaseService.getBoxInStorageInfo(wasteInPutStorageDto);
        return JsonForResult.<Map<String, Object>>builder().data(info).success(true).build();
    }

    @PostMapping("/bindBagWithBox")
    @ApiOperation("箱袋绑定")
    @ApiImplicitParam(name = "wasteInPutStorageDto", value = "医废入库详情", required = true, dataType = "WasteInPutStorageDto")
    public JsonForResult<String> bindBagWithBox(@RequestBody WasteInPutStorageDto wasteInPutStorageDto) {
        String boxId = wasteBaseService.bindBagWithBox(wasteInPutStorageDto);
        return JsonForResult.<String>builder().data(boxId).success(true).build();
    }

    @PostMapping("/removeBindBagWithBox")
    @ApiOperation("箱袋解除绑定")
    @ApiImplicitParam(name = "wasteInPutStorageDto", value = "医废入库详情", required = true, dataType = "WasteInPutStorageDto")
    public JsonForResult<String> removeBindBagWithBox(@RequestBody WasteInPutStorageDto wasteInPutStorageDto) {
        wasteBaseService.removeBindBagWithBox(wasteInPutStorageDto);
        return JsonForResult.<String>builder().success(true).build();
    }

    /**
     * 下载二维码
     *
     * @param id 医废ID
     * @return
     */
    @LoginWhitePathAnnotation
    @GetMapping("/generateQrCode/{id}")
    @ApiOperation("下载二维码")
    @ApiImplicitParam(name = "id", value = "医废id", required = true, dataType = "String")
    public JsonForResult<String> generateQrCode(@PathVariable String id) {
        String content = wasteBaseService.generateQrCode(id);
        return JsonForResult.<String>builder().data(content).success(true).build();
    }

    @PostMapping("/putSingleOutStorage")
    @ApiOperation("扫码出库")
    @ApiImplicitParam(name = "wasteOutPutStorageDto", value = "医废出库详情", required = true, dataType = "WasteOutPutStorageDto")
    public JsonForResult<String> putSingleOutStorage(@RequestBody WasteOutPutStorageDto wasteOutPutStorageDto) {
        wasteBaseService.putSingleOutStorage(wasteOutPutStorageDto);
        return JsonForResult.<String>builder().success(true).build();
    }

    @PostMapping("/putBoxOutStorage")
    @ApiOperation("箱入库")
    @ApiImplicitParam(name = "wasteOutPutStorageDto", value = "医废出库详情", required = true, dataType = "WasteOutPutStorageDto")
    public JsonForResult<String> putBoxOutStorage(@RequestBody WasteOutPutStorageDto wasteOutPutStorageDto) {
        wasteBaseService.putBoxOutStorage(wasteOutPutStorageDto);
        return JsonForResult.<String>builder().success(true).build();
    }

    @PostMapping("/printMessage")
    @ApiOperation("打印信息")
    @ApiImplicitParam(name = "wasteOutPutStorageDto", value = "医废出库详情", required = true, dataType = "WasteOutPutStorageDto")
    public JsonForResult<String> printMessage(@RequestBody WasteOutPutStorageDto wasteOutPutStorageDto) {
        wasteBaseService.printMessage(wasteOutPutStorageDto);
        return JsonForResult.<String>builder().success(true).build();
    }

    @PostMapping("/getPrintMessage")
    @ApiOperation("获取打印信息")
    public JsonForResult<Map<String, Object>> getPrintMessage(@RequestBody WasteOutPutStorageDto wasteOutPutStorageDto) {
        Map<String, Object> printMessage = wasteBaseService.getPrintMessage(wasteOutPutStorageDto);
        return JsonForResult.<Map<String, Object>>builder().data(printMessage).success(true).build();
    }

    @PostMapping("/getOrCodeType")
    @ApiOperation("获取二维码类型")
    @ApiImplicitParam(name = "qrCodeDto", value = "二位码信息", required = true, dataType = "QrCodeDto")
    public JsonForResult<String> getOrCodeType(@RequestBody QrCodeDto qrCodeDto) {
        EncryptDataUtil.EncryptData encryptData = EncryptDataUtil.decryptData(qrCodeDto.getContent());
        return JsonForResult.<String>builder().data(encryptData.getType()).success(true).build();
    }

    @PostMapping("/checkWarn")
    @ApiOperation("超重预警")
    @ApiImplicitParam(name = "wasteBaseDto", value = "医废信息传 id weight", required = true, dataType = "WasteBaseDto")
    public JsonForResult<String> checkWarn(@RequestBody WasteBaseDto wasteBaseDto) {
        String warn = wasteBaseService.checkWarnWeight(wasteBaseDto);
        return JsonForResult.<String>builder().data(warn).success(true).build();
    }

    @PostMapping("/getList")
    @ApiOperation("医废追溯-医废列表")
    @ApiImplicitParam(name = "wasteBaseDto", value = "医废信息传 status hospitalDepartId wasteClassifyCode collectTimeStart collectTimeEnd", required = true, dataType = "WasteBaseDto")
    public JsonForResult<List<WasteBaseVo>> getList(@RequestBody WasteBaseDto wasteBaseDto) {
        List<WasteBaseVo> list = wasteBaseService.getList(wasteBaseDto);
        Integer count = wasteBaseService.getCount(wasteBaseDto);
        return JsonForResult.<List<WasteBaseVo>>builder().data(list).total(count).success(true).build();
    }

    @GetMapping("/getCurrentCollectList")
    @ApiOperation("标签打印-医废列表")
    public JsonForResult<List<WasteBaseVo>> getCurrentCollectList() {
        List<WasteBaseVo> list = wasteBaseService.getCurrentCollectList();
        return JsonForResult.<List<WasteBaseVo>>builder().data(list).total(list.size()).success(true).build();
    }

    @PostMapping("/getListByCodes")
    @ApiOperation("医废列表")
    @ApiImplicitParam(name = "wasteBaseDto", value = "医废信息 codes", required = true, dataType = "WasteBaseDto")
    public JsonForResult<Map<String, Object>> getListByCodes(@RequestBody WasteBaseDto wasteBaseDto) {
        Map<String, Object> map = wasteBaseService.getListByCodes(wasteBaseDto.getCodes());
        return JsonForResult.<Map<String, Object>>builder().data(map).success(true).build();
    }

    @PostMapping("/getListByRFID")
    @ApiOperation("医废列表")
    @ApiImplicitParam(name = "wasteBaseDto", value = "医废信息 codes", required = true, dataType = "WasteBaseDto")
    public JsonForResult<Map<String, Object>> getListByRFID(@RequestBody WasteBaseDto wasteBaseDto) {
        Map<String, Object> map = wasteBaseService.getListByRFID(wasteBaseDto.getRfids());
        return JsonForResult.<Map<String, Object>>builder().data(map).success(true).build();
    }

    @PostMapping("/getListWithWeightByIds")
    @ApiOperation("医废列表 返回总重量")
    @ApiImplicitParam(name = "wasteBaseDto", value = "医废信息 ids", required = true, dataType = "WasteBaseDto")
    public JsonForResult<Map<String, Object>> getListByIds(@RequestBody WasteBaseDto wasteBaseDto) {
        Map<String, Object> map = wasteBaseService.getListWithWeightByIds(wasteBaseDto.getIds());
        return JsonForResult.<Map<String, Object>>builder().data(map).success(true).build();
    }


    @GetMapping("/getRecordList")
    @ApiOperation("医废追溯-医废记录列表")
    @ApiImplicitParam(name = "wasteBaseId", value = "医废id", required = true, dataType = "String")
    public JsonForResult<List<WasteBaseRecordVo>> getRecordList(String wasteBaseId) {
        List<WasteBaseRecordVo> recordVos = wasteBaseRecordService.getListByWasteBaseId(wasteBaseId);
        return JsonForResult.<List<WasteBaseRecordVo>>builder().data(recordVos).success(true).build();
    }

    @PostMapping("/getRecordListByQrCode")
    @ApiOperation("医废追溯-医废记录列表")
    @ApiImplicitParam(name = "qrCodeDto", value = "二位码信息", required = true, dataType = "QrCodeDto")
    public JsonForResult<List<WasteBaseRecordVo>> getRecordListByQrCode(@RequestBody QrCodeDto qrCodeDto) {
        List<WasteBaseRecordVo> recordVos = wasteBaseRecordService.getListByWasteBaseQrCode(qrCodeDto);
        return JsonForResult.<List<WasteBaseRecordVo>>builder().data(recordVos).success(true).build();
    }

    @PostMapping("/getInOrOutList")
    @ApiOperation("出入库记录")
    @ApiImplicitParam(name = "inOrOutDto", value = "出入库信息", required = true, dataType = "InOrOutDto")
    public JsonForResult<List<WasteBaseRecordVo>> getInOrOutList(@RequestBody InOrOutDto inOrOutDto) {
        List<WasteBaseRecordVo> list = wasteBaseRecordService.getInOrOutList(inOrOutDto);
        Integer count = wasteBaseRecordService.getInOrOutCount(inOrOutDto);
        return JsonForResult.<List<WasteBaseRecordVo>>builder().data(list).total(count).success(true).build();
    }


    @PostMapping("/getDepartByQrCode")
    @ApiOperation("二维码查询科室")
    @ApiImplicitParam(name = "qrCodeDto", value = "二位码信息", required = true, dataType = "QrCodeDto")
    public JsonForResult<HospitalDepartVo> getDepartByQrCode(@RequestBody QrCodeDto qrCodeDto) {
        HospitalDepartVo depart = wasteBaseService.getDepartByQrCode(qrCodeDto);
        return JsonForResult.<HospitalDepartVo>builder().data(depart).success(true).build();
    }

    @GetMapping("/getDepartByHospitalBaseId")
    @ApiOperation("科室列表")
    public JsonForResult<List<HospitalDepartVo>> getDepartByHospitalBaseId() {
        List<HospitalDepartVo> departs = wasteBaseService.getDepartByHospitalBaseId();
        return JsonForResult.<List<HospitalDepartVo>>builder().data(departs).success(true).build();
    }

    @GetMapping("/getDepartByParentId")
    @ApiOperation("科室列表")
    @ApiImplicitParam(name = "parentId", value = "上级科室id", required = true, dataType = "String")
    public JsonForResult<List<HospitalDepartVo>> getDepartByParentId(String parentId) {
        List<HospitalDepartVo> departs = wasteBaseService.getDepartByParentId(parentId);
        return JsonForResult.<List<HospitalDepartVo>>builder().data(departs).success(true).build();
    }

    @ApiOperation("预警-箱出库预警详情")
    @ApiImplicitParam(name = "boxRecordId", value = "箱记录id", required = true, dataType = "String")
    @GetMapping("/warnInfoByBoxRecordId")
    public JsonForResult<Map<String, Object>> warnInfoByBoxRecordId(String boxRecordId) {
        Map<String, Object> result = wasteBaseService.warnInfoByBoxRecordId(boxRecordId);
        return JsonForResult.<Map<String, Object>>builder().data(result).success(true).build();
    }

    @ApiOperation("出库单")
    @ApiImplicitParam(name = "wasteBaseDto", value = "医废信息", required = true, dataType = "WasteBaseDto")
    @PostMapping("getWasteOutList")
    public JsonForResult<List<WasteOutVo>> getWasteOutList(@RequestBody WasteBaseDto wasteBaseDto){
        List<WasteOutVo> list = wasteBaseService.getWasteOutList(wasteBaseDto);
        Integer count = wasteBaseService.getWasteOutCount(wasteBaseDto);
        return JsonForResult.<List<WasteOutVo>>builder().data(list).total(count).success(true).build();
    }

    @ApiOperation("出库单详情")
    @ApiImplicitParam(name = "wasteOutId", value = "出库id", required = true, dataType = "String")
    @GetMapping("/getWasteOutInfo")
    public JsonForResult<List<WasteBaseVo>> getWasteOutInfo(String wasteOutId) {
        List<WasteBaseVo> info = wasteBaseService.getWasteOutInfo(wasteOutId);
        return JsonForResult.<List<WasteBaseVo>>builder().data(info).success(true).build();
    }

}
